package com.zzyl.nursing.controller.member;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.domain.DeviceDataQuery;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.ElderlyPerson;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.impl.AlertDataServiceImpl;
import com.zzyl.nursing.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 老人家属Controller
 *
 * @author alexis
 * @date 2025-01-09
 */
@Api(tags = "老人家属管理")
@RestController
@RequestMapping("/member/user")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private IDeviceDataService deviceDataService;
    @Autowired
    private IAlertDataService alertDataService;


    @PostMapping("/login")
    public R<LoginVo> login(@RequestBody UserLoginRequestDto userLoginRequestDto) {
        // 调用Service层接口进行小程序登录
        LoginVo loginVo = familyMemberService.login(userLoginRequestDto);
        return R.ok(loginVo);
    }

    //绑定家属
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ElderlyPerson elderlyPerson) {
        familyMemberService.add(elderlyPerson);
        return AjaxResult.success();
    }

    /**
     * 查询家人列表
     *
     * @return
     */
    @GetMapping("/my")
    public R<List<ElderlyFamilyMemberVO>> my() {

        return R.ok(familyMemberService.getMyelderlyPersonList());
    }

    @GetMapping("list-by-page")
    public R<List<ElderlyFamilyMemberRoomVO>> listByPage(@RequestParam(defaultValue = "1") int pageNum,
                                                         @RequestParam(defaultValue = "10") int pageSize) {
        return R.ok(familyMemberService.listByPage(pageNum, pageSize));
    }

    @PostMapping("/QueryDevicePropertyStatus")
    public AjaxResult QueryDevicePropertyStatus(@RequestBody Device device) {
        return familyMemberService.QueryDevicePropertyStatus(device);
    }

    @GetMapping("/queryDeviceDataListByDay")
    public R queryDeviceDataListByDay(@RequestParam String iotId,
                                      @RequestParam String functionId,
                                      @RequestParam long startTime,
                                      @RequestParam long endTime) {
        DeviceDataQuery query = new DeviceDataQuery(iotId, functionId, startTime, endTime);
        return R.ok(deviceDataService.getAverageDataValueByHour(query));

    }

    @GetMapping("queryDeviceDataListByWeek")
    public R queryDeviceDataListByWeek(@RequestParam String iotId,
                                       @RequestParam String functionId,
                                       @RequestParam long startTime,
                                       @RequestParam long endTime) {
        DeviceDataQuery query = new DeviceDataQuery(iotId, functionId, startTime, endTime);
        return R.ok(deviceDataService.getAverageDataValueByWeek(query));
    }

    @GetMapping("pageQueryAlertData")
    public TableDataInfo<AlertData> list(@ApiParam("报警数据查询条件") String functionId, Long startTime, Long endTime, String iotId, int pageNum, int pageSize) {
        Page<AlertData> alertDataPage = alertDataService.pageQueryAlertData(functionId, startTime, endTime, iotId, pageNum, pageSize);
        TableDataInfo<AlertData> alertDataTableDataInfo = new TableDataInfo<>();
        alertDataTableDataInfo.setRows(alertDataPage.getRecords());
        alertDataTableDataInfo.setTotal((int) alertDataPage.getTotal());
        alertDataTableDataInfo.setCode(200);
        alertDataTableDataInfo.setMsg("查询成功");
        return alertDataTableDataInfo;
    }
}
